#!/bin/bash

## the informal map-reduce shell scripts
## usage:
## mapred.sh <component1 component2]> <scripts> <env> <user> <workdir>

comps="$1"
scripts="$2"
env=$3
user=${4}
workdir=$5

if [ "${comps}" = "" ];
then
echo "Usage: mapred.sh <component1 component2> <scripts> <env> <user> <workdir>"
echo "	Use blank space to seperate the multiple components"
echo "	env: optional. Default env is ec2-cv-prod"
echo "	user: optional.Default user is administrator"
echo "	workdir: optional. Specify the work dir, default will use a ramdom dir"
exit
fi

if [ "$env" == "" ];
then
env=ec2-cv-prod
fi

if [ "${user}" == "" ];
then
user=administrator
fi

# build the server file for specified components

#prepare a work dir for this mapred task
if [ "${workdir}" == "" ];
then
workdir=./`date '+%s'`
echo "INFO: use a random work dir - ${workdir}"
else
echo "INFO:	use the specified work dir - ${workdir}"
fi
mkdir -p "${workdir}"
workdir=`./ab-dir.sh ${workdir}`

./prepare.sh "${workdir}" "${scripts}" "$env" "${comps}"
serverfile="${workdir}"/server.txt
sshkeyfile="${workdir}"/ssh_key
scriptfile="${workdir}"/map.sh

# send the script to remote server, and execute it
./map_servers.sh "${user}" "${serverfile}" "${workdir}" "${scriptfile}" "${sshkeyfile}"